package com.jinsi.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jinsi.listener.StuListener;
import com.jinsi.pojo.*;
import com.jinsi.service.impl.ScStuServiceImpl;
import com.jinsi.service.impl.StuOperationLogServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@Controller
public class StuController {
    @Autowired
    private ScStuServiceImpl service;
    @Autowired
    private StuOperationLogServiceImpl logServer;

    @ResponseBody
    @RequestMapping("/getStus")
    public Map<String,Object> getStus(Integer page, Integer limit){
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page,limit);
        List<ScStudent> list = service.getStus();
        PageInfo<ScStudent> pageInfo = new PageInfo<>(list);
        map.put("total",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        map.put("state",0);
        map.put("msg","成功");
        return map;
    }


    @ResponseBody
    @RequestMapping("/getStusByLikeIdAndName")
    public Map<String,Object> getStusByLikeIdAndName(
            Integer studentId,
            String studentName,
            Integer status,
            Integer page,Integer limit){
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page,limit);
        List<ScStudentOperationLog> list = service.getStusByLikeIdAndName(
                studentId,
                studentName,
                status
        );
        PageInfo<ScStudentOperationLog> pageInfo = new PageInfo<>(list);
        map.put("total",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        map.put("state",0);
        map.put("msg","成功");
        return map;
    }


    @ResponseBody
    @RequestMapping("/getStusByLike")
    public Map<String,Object> getStusByLike(
            Integer studentId,
            String studentName,
            String studentIdNumber,
            String studentCreateDate,
            Integer page,Integer limit){
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page,limit);
        List<ScStudent> list = service.getStusByLike(
                studentId,
                studentName,
                studentIdNumber,
                studentCreateDate
        );
        PageInfo<ScStudent> pageInfo = new PageInfo<>(list);
        map.put("total",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        map.put("state",0);
        map.put("msg","成功");
        return map;
    }


    @ResponseBody
    @PostMapping("/getStudentStandard")
    public List<Map<String,Object>> getStudentStandard(String studentId){
        List<Map<String,Object>> tree = service.getStudentStandard(studentId);
        return tree;
    }




    @ResponseBody
    @RequestMapping("/delStu")
    public Map<String,String> delStu(String studentId,String studentName){
        Map<String,String> map = new HashMap<>();
        try {
            service.delStu(studentId);
            ScStudentOperationLog log = new ScStudentOperationLog();
            log.setStudentId(studentId);
            log.setStudentName(studentName);
            log.setOperationDateTime(new Date());
            log.setOperationRemark("删除学生信息");
            log.setStatus(1);
            logServer.addLog(log);
            map.put("state","200");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state","400");
        }
        return map;
    }



    @ResponseBody
    @RequestMapping("/getStuOperationLogByOne")
    public Map<String,Object> getStuOperationLogByOne(Integer page,Integer limit){
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page,limit);
        List<ScStudentOperationLog> list = logServer.getStuOperationLog(1);
        PageInfo<ScStudentOperationLog> pageInfo = new PageInfo<>(list);
        map.put("total",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        map.put("state",0);
        map.put("msg","成功");
        return map;
    }

    @ResponseBody
    @RequestMapping("/getStuOperationLogByTwo")
    public Map<String,Object> getStuOperationLogByTwo(Integer page,Integer limit){
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page,limit);
        List<ScStudentOperationLog> list = logServer.getStuOperationLog(2);
        PageInfo<ScStudentOperationLog> pageInfo = new PageInfo<>(list);
        map.put("total",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        map.put("state",0);
        map.put("msg","成功");
        return map;
    }

    @ResponseBody
    @RequestMapping("/getStuOperationLogByThree")
    public Map<String,Object> getStuOperationLogByThree(Integer page,Integer limit){
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page,limit);
        List<ScStudentOperationLog> list = logServer.getStuOperationLog(3);
        PageInfo<ScStudentOperationLog> pageInfo = new PageInfo<>(list);
        map.put("total",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        map.put("state",0);
        map.put("msg","成功");
        return map;
    }


    @ResponseBody
    @RequestMapping("/delStuOperationLog")
    public Map<String,String> delStuOperationLog(Integer id){
        Map<String, String> map = new HashMap<>();
        try {
            logServer.delStuOperationLog(id);
            map.put("state","200");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state","400");
        }
        return map;
    }


    @ResponseBody
    @RequestMapping("/upStuByStatus")
    public Map<String,String> upStuByStatus(ScStudent student){
        Map<String, String> map = new HashMap<>();
        try {
            student.setStudentUpdateDate(new Date());
            service.upStuByStatus(student);

            ScStudentOperationLog log = new ScStudentOperationLog();
            log.setStudentId(student.getStudentId());
            log.setStudentName(student.getStudentName());
            log.setOperationDateTime(new Date());
            log.setStatus(3);
            if (student.getStudentStatus() == 2){
                log.setOperationRemark("学生状态--毕业");
            }else if (student.getStudentStatus() == 3){
                log.setOperationRemark("学生状态--休学");
            }else if (student.getStudentStatus() == 4){
                log.setOperationRemark("学生状态--退学");
            }else {
                log.setOperationRemark("学生状态--在线");
            }
            logServer.addLog(log);
            map.put("state","200");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state","400");
        }
        return map;
    }


    @ResponseBody
    @RequestMapping("/drStu")
    public Map<String,String> drStu(MultipartFile file) throws IOException {
        System.out.println("已进入");

        InputStream inputStream = file.getInputStream();

        EasyExcel.read(inputStream,ScStudent.class,new StuListener(service)).sheet().doRead();
        Map<String,String> map = new HashMap();
        map.put("state","200" );

        return map;
    }


    @ResponseBody
    @RequestMapping("/dcStu")
    public Map<String,Object> dcStu(@RequestBody List<ScStudent> list){
        Map<String,Object> map = new HashMap();
        for (ScStudent i : list){
            System.out.println(i);
        }
        int max=10000;
        int min=1000;
        Random random = new Random();
        int count =  (random.nextInt(max - min) + min + 1);
        System.out.println(count);
        String fileName = "导出学生信息" + count +".xlsx";
        EasyExcel.write(fileName,ScStudent.class).sheet("学生信息").doWrite(list);
        map.put("state","200");
        return map;
    }


    @ResponseBody
    @RequestMapping("/dcStuOperationLog")
    public Map<String,String> dcStuOperationLog(@RequestBody List<ScStudentOperationLog> list){
        Map<String,String> map = new HashMap();
        for (ScStudentOperationLog i : list){
            System.out.println(i);
        }

        String fileName = "C:\\Users\\腾gg\\Desktop\\导出学生操作日志信息.xlsx";
        EasyExcel.write(fileName,ScStudentOperationLog.class).sheet("学生操作日志信息").doWrite(list);
        map.put("state","200" );

        return map;
    }


    @ResponseBody
    @RequestMapping("/dcDefer")
    public Map<String,String> dcDefer(@RequestBody List<ScStudentDefer> list){
        Map<String,String> map = new HashMap();
        for (ScStudentDefer i : list){
            System.out.println(i);
        }

        String fileName = "C:\\Users\\腾gg\\Desktop\\导出学生缓交信息.xlsx";
        EasyExcel.write(fileName,ScStudentDefer.class).sheet("学生缓交信息").doWrite(list);
        map.put("state","200" );

        return map;
    }

    @ResponseBody
    @RequestMapping("/dcBonus")
    public Map<String,String> dcBonus(@RequestBody List<ScStudentBonus> list){
        Map<String,String> map = new HashMap();
        for (ScStudentBonus i : list){
            System.out.println(i);
        }

        String fileName = "C:\\Users\\腾gg\\Desktop\\导出学生奖金信息.xlsx";
        EasyExcel.write(fileName,ScStudentBonus.class).sheet("学生奖金信息").doWrite(list);
        map.put("state","200" );

        return map;
    }

    @ResponseBody
    @RequestMapping("/upStuByRecoverOrDropOut")
    public Map<String,String> upStuByRecoverOrDropOut(ScStudent student,ScStudentOperationLog log){
        Map<String, String> map = new HashMap<>();
        student.setStudentUpdateDate(new Date());
        log.setOperationDateTime(new Date());
        try {
            service.upStuByStatus(student);
            logServer.upLog(log);
            map.put("state","200");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state","400");
        }
        return map;
    }

}
